package com.zch.market.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zch.market.dao.BillMapper;
import com.zch.market.entity.Bill;
import com.zch.market.service.BillService;
@Service("bs")
public class BillServiceImpl implements BillService {
	@Autowired
	private BillMapper dao;
	//每页中存放的数据个数
	private final int PAGE_SIZE=4;
	@Override
	public List<Bill> getBillByPage(String billName, int isPay, int pageIndex) {
		//使用集合来存放可变参数
		Map<String, Object> map = new HashMap<>();
		map.put("billName", billName);
		map.put("isPay", isPay);
		//获得当前页第一条数据在查询的数据中的位置
		int firstRow = (pageIndex - 1)*PAGE_SIZE+1;
		//获得当前页最后一条数据在查询的数据中的位置
		int lastRow = pageIndex*PAGE_SIZE;
		map.put("firstRow", firstRow);
		map.put("lastRow", lastRow);
		//获得查询的所有数据
		List<Bill> list = dao.getBillByPage(map);
		return list;
	}

	@Override
	public int getTotalPage(String billName, int isPay) {
		//使用集合来存放可变参数
		Map<String, Object> map = new HashMap<>();
		map.put("billName", billName);
		map.put("isPay", isPay);
		int counts = dao.getTotalCount(map);
		//获得总页数
		int pageCounts = counts%PAGE_SIZE==0?counts/PAGE_SIZE:counts/PAGE_SIZE + 1;
		return pageCounts;
	}
	@Override
	public int getTotalCounts(String billName, int isPay) {
		//使用集合来存放可变参数
		Map<String, Object> map = new HashMap<>();
		map.put("billName", billName);
		map.put("isPay", isPay);
		int counts = dao.getTotalCount(map);
		return counts;
	}

	@Override
	public Bill getBillById(int id) {
		
		return dao.getBillById(id);
	}

	@Override
	public boolean addBill(Bill bill) {
		
		return dao.addBill(bill);
	}

	@Override
	public boolean deleteBill(int id) {
		
		return dao.deleteBill(id);
	}

	@Override
	public boolean updateBill(Bill bill) {
		
		return dao.updateBill(bill);
	}

}
